<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item size="mini" label="" prop="dictName">
        <el-input
          v-model="queryParams.cfmajormatters"
          placeholder="请输入主要事项"
          clearable
          class="search-form-input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-date-picker
          v-model="queryParams.cfyear"
          type="year"
          value-format="yyyy"
          format="yyyy"
          class="search-form-input"
          placeholder="选择年"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-date-picker
          v-model="queryParams.cfmonth"
          type="month"
          value-format="M"
          format="M"
          class="search-form-input"
          placeholder="选择月"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-date-picker
          v-model="queryParams.cfday"
          type="date"
          value-format="d"
          format="d"
          class="search-form-input"
          placeholder="选择日"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-select
          v-model="queryParams.cfplanstate"
          placeholder="计划状态"
          clearable
          class="search-form-input"
        >
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item size="mini">
        <el-select
          v-model="queryParams.cfstate"
          placeholder="日志状态"
          clearable
          class="search-form-input"
        >
          <el-option key="1" label="保存" value="1" />
          <el-option key="2" label="提交" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item size="mini">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
        <el-button
          v-hasPermi="['system:dict:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="ids.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" size="small" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="提交人"
        align="center"
        prop="creatorName"
        width="120"
      />
      <el-table-column label="主要事项" align="center" min-width="120">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="click"
          >
            <div class="ellipsis-all-content">{{ scope.row.cfmajormatters }}</div>
            <div slot="reference" class="ellipsis">{{ scope.row.cfmajormatters }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <!--      <el-table-column label="领导协调" align="center" min-width="60">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-popover-->
      <!--            placement="top-start"-->
      <!--            trigger="click"-->
      <!--          >-->
      <!--            <div class="ellipsis-all-content">{{ scope.row.cfcoordinating }}</div>-->
      <!--            <div slot="reference" class="ellipsis">{{ scope.row.cfcoordinating }}</div>-->
      <!--          </el-popover>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="安全隐患" align="center" min-width="60">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-popover-->
      <!--            placement="top-start"-->
      <!--            trigger="click"-->
      <!--          >-->
      <!--            <div class="ellipsis-all-content">{{ scope.row.cfsecuritythreat }}</div>-->
      <!--            <div slot="reference" class="ellipsis">{{ scope.row.cfsecuritythreat }}</div>-->
      <!--          </el-popover>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="其他" align="center" min-width="60">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-popover-->
      <!--            placement="top-start"-->
      <!--            trigger="click"-->
      <!--          >-->
      <!--            <div class="ellipsis-all-content">{{ scope.row.cfother }}</div>-->
      <!--            <div slot="reference" class="ellipsis">{{ scope.row.cfother }}</div>-->
      <!--          </el-popover>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="日期" align="center" min-width="60">
        <template slot-scope="scope">
          <span>{{ scope.row.cfyear }}-{{ scope.row.cfmonth }}-{{ scope.row.cfday }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作日" align="center" min-width="50">
        <template slot-scope="scope">
          <span>{{ scope.row.cfweekday | weekday }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划状态" align="center" prop="cfplanstate" min-width="50">
        <template slot-scope="scope">
          <span>{{ scope.row.cfplanstate | planstate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="cfplanstate" min-width="50">
        <template slot-scope="scope">
          <span>{{ scope.row.cfstate == 1 ? '保存' : '提交' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="fcreatetime"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.fcreatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="最后修改时间"
        align="center"
        prop="flastupdatetime"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.flastupdatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:dict:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showInfo(scope.row)"
          >详情
          </el-button>
          <el-button
            v-hasPermi="['system:dict:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body @close="reset">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="date">
          <el-date-picker
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            :disabled="!isAdd"
            @change="dateChange"
          />
        </el-form-item>
        <el-form-item label="状态" prop="cfplanstate">
          <el-radio-group v-model="form.cfplanstate">
            <el-radio
              v-for="dict in status"
              :key="dict.value"
              :label="dict.value"
              :disabled="!isAdd"
            >{{ dict.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="主要事项" prop="cfmajormatters">
          <el-input v-model="form.cfmajormatters" :disabled="!isAdd" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" placeholder="请输入日工作主要事项" />
        </el-form-item>
        <el-form-item label="领导协调" prop="cfcoordinating">
          <el-input v-model="form.cfcoordinating" :disabled="!isAdd" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" placeholder="请输入需领导协调事项" />
        </el-form-item>
        <el-form-item label="安全隐患" prop="cfsecuritythreat">
          <el-input v-model="form.cfsecuritythreat" :disabled="!isAdd" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" placeholder="请输入可能存在重大质量或安全隐患" />
        </el-form-item>
        <el-form-item label="其他" prop="cfother">
          <el-input v-model="form.cfother" :disabled="!isAdd" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" placeholder="请输入其他" />
        </el-form-item>
      </el-form>
      <div v-if="isAdd" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(2)">保存并提交</el-button>
        <el-button type="primary" @click="submitForm(1)">保存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkLog, delWorkLog, addWorkLog } from '@/api/baseManage/workLog'

export default {
  name: 'WorkLog',
  filters: {
    planstate(val) {
      if (val === 1) {
        return '正常'
      } else if (val === 2) {
        return '滞后'
      } else if (val === 3) {
        return '提前'
      }
    },
    weekday(val) {
      const arrWork = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
      return arrWork[val - 1]
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      status: [
        { name: '正常', value: 1 },
        { name: '滞后', value: 2 },
        { name: '提前', value: 3 }
      ],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cfmajormatters: '',
        cfyear: '',
        cfmonth: '',
        cfday: '',
        cfplanstate: ''
      },
      // 表单参数
      form: {
        date: '',
        cfplanstate: 1,
        cfmajormatters: '',
        cfcoordinating: '',
        cfsecuritythreat: '',
        cfother: '',
        cfyear: '',
        cfmonth: '',
        cfday: '',
        cfweekday: '',
        cfstate: 1
      },
      isAdd: true,
      // 表单校验
      rules: {
        date: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        cfplanstate: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询工作日志列表 */
    getList() {
      this.loading = true
      listWorkLog(this.queryParams).then(response => {
        this.typeList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    dateChange(val) {
      console.log(val)
      const day = new Date(val).getDay()
      if (day === 0) {
        this.form.cfweekday = 7
      } else {
        this.form.cfweekday = day
      }
      const dateArr = val.split('-')
      this.form.cfyear = dateArr[0]
      this.form.cfmonth = dateArr[1]
      this.form.cfday = dateArr[2]
    },
    // 显示详情
    showInfo(row) {
      this.open = true
      this.isAdd = false
      this.form = { ...row }
      this.form.date = row.cfyear + '-' + row.cfmonth + '-' + row.cfday
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.$refs.form.resetFields()
      this.form = {
        date: '',
        cfplanstate: 1,
        cfmajormatters: '',
        cfcoordinating: '',
        cfsecuritythreat: '',
        cfother: '',
        cfyear: '',
        cfmonth: '',
        cfday: '',
        cfweekday: '',
        cfstate: 1
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        cfmajormatters: '',
        cfyear: '',
        cfmonth: '',
        cfday: '',
        cfplanstate: ''
      }
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.isAdd = true
      this.title = '添加工作日志'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
    },
    /** 提交按钮 */
    submitForm: function(cfstate) {
      this.form.cfstate = cfstate
      this.$refs['form'].validate(valid => {
        if (valid) {
          addWorkLog(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess('新增成功')
              this.open = false
              this.reset()
              this.getList()
            }
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fids = this.ids.length > 0 ? this.ids : [row.fid]
      this.$confirm('是否要删除操作的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delWorkLog(fids)
      }).then(() => {
        this.getList()
        this.ids = []
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
<style scoped lang="scss">
  .search-form-input {
    width: 130px;
  }
</style>
